package cn.laike.modules.dev.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.laike.common.validator.ValidatorUtils;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.laike.modules.dev.entity.BreakfastCouponEntity;
import cn.laike.modules.dev.entity.EarnestMoneyOrderEntity;
import cn.laike.modules.dev.entity.HotelEntity;
import cn.laike.modules.dev.entity.OrderEntity;
import cn.laike.modules.dev.service.BreakfastCouponService;
import cn.laike.modules.dev.service.EarnestMoneyOrderService;
import cn.laike.modules.dev.service.HotelService;
import cn.laike.modules.dev.service.OrderService;
import cn.laike.modules.dev.utils.DevUtils;
import cn.laike.modules.sys.entity.SysUserEntity;
import cn.laike.modules.sys.shiro.ShiroUtils;
import cn.laike.common.utils.DateUtils;
import cn.laike.common.utils.PageUtils;
import cn.laike.common.utils.R;



/**
 * 早餐券记录
 *
 * @author ziye
 * @email 524748907@qq.com
 * @date 2019-03-02 11:23:33
 */
@RestController
@RequestMapping("dev/breakfastcoupon")
public class BreakfastCouponController {
    @Autowired
    private BreakfastCouponService breakfastCouponService;
    
    @Autowired
    private EarnestMoneyOrderService earnestMoneyOrderService;
    
    @Autowired
    private OrderService orderService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("dev:breakfastcoupon:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = breakfastCouponService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("dev:breakfastcoupon:info")
    public R info(@PathVariable("id") Integer id){
        BreakfastCouponEntity breakfastCoupon = breakfastCouponService.selectById(id);

        return R.ok().put("breakfastCoupon", breakfastCoupon);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("dev:breakfastcoupon:save")
    public R save(@RequestBody BreakfastCouponEntity breakfastCoupon){
        breakfastCouponService.insert(breakfastCoupon);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("dev:breakfastcoupon:update")
    public R update(@RequestBody BreakfastCouponEntity breakfastCoupon){
        ValidatorUtils.validateEntity(breakfastCoupon);
        breakfastCouponService.updateAllColumnById(breakfastCoupon);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("dev:breakfastcoupon:delete")
    public R delete(@RequestBody Integer[] ids){
        breakfastCouponService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }
    
    
    @RequestMapping("/giveCoupon")
    public R giveCoupon(@RequestParam(value="orderId")Integer orderId,@RequestParam(value="days")Integer days,
    		@RequestParam(value="number")Integer number){
    	if(orderId != null && orderId > 0) {
			System.out.println(orderId);
        	System.out.println(days);
        	System.out.println(number);
        	SysUserEntity user = ShiroUtils.getUserEntity();
        	if(user.getHotelId() != null && user.getHotelId() >0) {
        		EarnestMoneyOrderEntity order = earnestMoneyOrderService.selectById(orderId);
        		for (int i = 0; i < days; i++) {
    				for (int j = 0; j < number; j++) {
    					BreakfastCouponEntity breakfastCoupon = new BreakfastCouponEntity();
    					breakfastCoupon.setUid(order.getUid());
    					breakfastCoupon.setHotelId(order.getHotelId());
    					breakfastCoupon.setHotelName(order.getHotelName());
    					breakfastCoupon.setHotelRoomId(0);
    					breakfastCoupon.setHotelRoomName(order.getHotelRoomName());
    					breakfastCoupon.setOpenid(order.getOpenid());
    					breakfastCoupon.setName(order.getName());
    					breakfastCoupon.setNickname(order.getNickname());
    					breakfastCoupon.setName(order.getName());
    					breakfastCoupon.setName(order.getName());
    					breakfastCoupon.setMobile(order.getMobile());
    					breakfastCoupon.setAvatar(order.getAvatar());
    					breakfastCoupon.setStatus(0);
    					breakfastCoupon.setUsedDate(DateUtils.addDateDays(new Date(), i));
    					breakfastCoupon.setCreateTime(new Date());
    					breakfastCouponService.insert(breakfastCoupon);
    				}
    			}
        		System.out.println("押金赠送早餐券....");
        		DevUtils.sendTemplateMessage(order.getUid(), order.getOpenid(),order.getPrepayId());
        		return R.ok();
        	}else {
        		return R.error("你无权设置");
        	}	
    	}else {
    		return R.error("参数异常");
    	}
    	
    }
    
    @RequestMapping("/giveOrderCoupon")
    public R giveOrderCoupon(@RequestParam(value="orderId")Integer orderId,@RequestParam(value="days")Integer days,
    		@RequestParam(value="number")Integer number){
    	if(orderId != null && orderId > 0) {
			System.out.println(orderId);
        	System.out.println(days);
        	System.out.println(number);
        	SysUserEntity user = ShiroUtils.getUserEntity();
        	if(user.getHotelId() != null && user.getHotelId() >0) {
        		OrderEntity order = orderService.selectById(orderId);
        		for (int i = 0; i < days; i++) {
    				for (int j = 0; j < number; j++) {
    					BreakfastCouponEntity breakfastCoupon = new BreakfastCouponEntity();
    					breakfastCoupon.setUid(order.getUid());
    					breakfastCoupon.setHotelId(order.getHotelId());
    					breakfastCoupon.setHotelName(order.getHotelName());
    					breakfastCoupon.setHotelRoomId(order.getHotelRoomId());
    					breakfastCoupon.setHotelRoomName(order.getHotelRoomName());
    					breakfastCoupon.setOpenid(order.getOpenid());
    					breakfastCoupon.setName(order.getName());
    					breakfastCoupon.setNickname(order.getNickname());
    					breakfastCoupon.setName(order.getName());
    					breakfastCoupon.setName(order.getName());
    					breakfastCoupon.setMobile(order.getMobile());
    					breakfastCoupon.setAvatar(order.getAvatar());
    					breakfastCoupon.setStatus(0);
    					breakfastCoupon.setUsedDate(DateUtils.addDateDays(new Date(), i));
    					breakfastCoupon.setCreateTime(new Date());
    					breakfastCouponService.insert(breakfastCoupon);
    				}
    			}
        		System.out.println("预订赠送早餐券....");
        		DevUtils.sendTemplateMessage(order.getUid(), order.getOpenid(),order.getPrepayId());
        		return R.ok();
        	}else {
        		return R.error("你无权设置");
        	}	
    	}else {
    		return R.error("参数异常");
    	}
    	
    }

    
    /**
     * 统计
     * @param params
     * @return
     */
    @RequestMapping("/statTotalMoney")
    public R getmoneylist(@RequestParam Map<String, Object> params){
    	String startTime = (String)params.get("startTime");
    	String endTime = (String)params.get("endTime");
    	if(StringUtils.isEmpty(startTime)){
    		startTime = DateUtils.format(new Date());
    		params.put("startTime", startTime);
    	}
    	if(StringUtils.isEmpty(endTime)){
    		endTime = DateUtils.format(new Date());
    		params.put("endTime", endTime);
    	}
    	
        List<Double> moneylist = breakfastCouponService.statTotalMoney(ShiroUtils.getUserId(),ShiroUtils.getHotelId(), startTime, endTime); //donationOrderService.statOrderMoney(ShiroUtils.getUserId(), startTime, endTime);
        return R.ok().put("moneylist", moneylist).put("startTime", startTime).put("endTime", endTime);
    }
}
